﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    /// <summary>
    /// 手风琴控件
    /// </summary>
    public class PageCommon_Accordion:ContainerBase
    {
        private string _id = "";

        public PageCommon_Accordion(ViewContext viewContext) : base(viewContext)
        {
            this.Css("box-group");
        }

        /// <summary>
        /// 输出一个子控件,使用using
        /// </summary>
        /// <returns></returns>
        public AccordionItem RenderBox()
        {
            return new AccordionItem(ViewContext, _id);
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- .box-group -->");
        }

        internal override ContainerBase RenderInternal()
        {
            _id = Attributes.GetValue("id", "");

            if (string.IsNullOrWhiteSpace(_id))
            {
                _id = "boxaccordion_" + AdminLTEUI.NextControlIndex();
                Attributes.ID(_id);

            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            return this;
        }

        public class AccordionItem : ContainerBase
        {
            private ColorPaletteEnum _styleColor = ColorPaletteEnum.@default;
            private string _title = "";
            private bool _isActive = false;
            private string _accordionID = "";

            public AccordionItem(ViewContext viewContext,string accordionID) : base(viewContext)
            {
                this.Css("panel box");
                _accordionID = accordionID;
            }

            private AccordionItem Active(bool isActive=true)
            {
                _isActive = isActive;
                return this;
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- .box-body -->");
                ViewContext.Writer.WriteLine("</div> <!-- .panel-collapse -->");

                ViewContext.Writer.WriteLine("</div> <!-- .panel -->");
            }

            internal override ContainerBase RenderInternal()
            {
                this.Css("box-" + _styleColor.ToString());

                var id = Attributes.GetValue("id", "");

                if (string.IsNullOrWhiteSpace(id))
                {
                    id = "boxaccordionitem_" + AdminLTEUI.NextControlIndex();
                    Attributes.ID(id);
                }

                if (_withOuterBorder)
                {
                    ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
                }

                ViewContext.Writer.WriteLine($"<div class=\"panel box box-primary\">");
                ViewContext.Writer.WriteLine("<div class=\"box-header with-border\">");
                ViewContext.Writer.WriteLine($"<h4 class=\"box-title\"><a data-toggle=\"collapse\" data-parent=\"#{_accordionID}\" href=\"#{id}\">{_title}</a></h4>");
                ViewContext.Writer.WriteLine("</div>");

                ViewContext.Writer.WriteLine($"<div id=\"{id}\" class=\"panel-collapse collapse {_isActive.If("in")}\">");

                ViewContext.Writer.WriteLine("<div class=\"box-body\">");

                if (_withOuterBorder)
                {
                    ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
                }

                return this;
            }
        }
    }
}
